package com.shop2cn.iapi.couponmanage.testcase.old;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.coupon.parameter.UserReceivePackageBean;
import com.shop2cn.iapi.coupon.parameter.userReceiveCouponBagBean;
import com.shop2cn.iapi.coupon.service.userReceiveCouponBagCall;
import com.shop2cn.iapi.couponmanage.parameter.CreateBagBean;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.TaskInfo;
import com.shop2cn.iapi.couponmanage.parameter.createTaskByUserIdBean;
import com.shop2cn.iapi.couponmanage.parameter.failSendRecord;
import com.shop2cn.iapi.couponmanage.parameter.querySendFailDetailBean;
import com.shop2cn.iapi.couponmanage.service.CreateBagCall;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.shop2cn.iapi.couponmanage.service.createTaskByUserIdCall;
import com.shop2cn.iapi.couponmanage.service.querySendFailDetailCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.model.couponsendsubtask;
import com.ymttest.database.model.couponsendtask;
import com.ymttest.database.model.couponsendtaskfailrecord;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.database.sqlwapper.couponsendsubtaskWapper;
import com.ymttest.database.sqlwapper.couponsendtaskWapper;
import com.ymttest.database.sqlwapper.couponsendtaskfailrecordWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*查询发放失败明细
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_querySendFailDetail{
	private static querySendFailDetailBean querysendfaildetailBean;
	private static querySendFailDetailCall querysendfaildetailCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询发放失败明细");
	}

	@Before
	public void caseUp() {
		querysendfaildetailBean=new querySendFailDetailBean();
		querysendfaildetailCall=new querySendFailDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querysendfaildetailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_querySendFailDetail_001() {
		Logger.start(true,"获取任务信息-普通券");
		try {
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			createTaskByUserIdBean createTaskByUserIdBean=couponCallService.setCreateTaskByUserIdBeanForFailPlatform();
			
			List<Integer> userIds=new ArrayList<Integer>();
			createTaskByUserIdCall taskcall=couponCallService.couponsendsubtaskFail(createTaskByUserIdBean);
			Thread.sleep(2000);
			querysendfaildetailBean.setTaskId(taskcall.getTaskId());
			querysendfaildetailBean.setAppId("testAuto");
			querysendfaildetailBean.setRequestId("testAuto");
			querysendfaildetailCall.setData(querysendfaildetailBean);
			querysendfaildetailCall.callService();			
			verifyReturn(taskcall.getTaskId());		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_querySendFailDetail_002() {
		Logger.start(true,"获取任务信息-套券");
		try {
			createTaskByUserIdCall taskcall= setCreateTaskByUserIdBeanForFailBagPlatform();
			Thread.sleep(3000);
			querysendfaildetailBean.setTaskId(taskcall.getTaskId());
			querysendfaildetailBean.setAppId("testAuto");
			querysendfaildetailBean.setRequestId("testAuto");
			querysendfaildetailCall.setData(querysendfaildetailBean);
			querysendfaildetailCall.callService();			
			verifyReturn(taskcall.getTaskId());			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_querySendFailDetail_003() {
		Logger.start(true,"获取任务信息-TaskId不存在");
		try {
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			createTaskByUserIdBean createTaskByUserIdBean=couponCallService.setCreateTaskByUserIdBeanForFailPlatform();
			
			List<Integer> userIds=new ArrayList<Integer>();
			createTaskByUserIdCall taskcall=couponCallService.couponsendsubtaskFail(createTaskByUserIdBean);
			Thread.sleep(2000);
			querysendfaildetailBean.setTaskId(taskcall.getTaskId()+1);
			querysendfaildetailBean.setAppId("testAuto");
			querysendfaildetailBean.setRequestId("testAuto");
			querysendfaildetailCall.setData(querysendfaildetailBean);
			querysendfaildetailCall.callService();	
			
			Logger.verifyEquals("0", querysendfaildetailCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", querysendfaildetailCall.getMessage(), "验证返回Message");
			List<failSendRecord> infos = querysendfaildetailCall.getfailSendRecord();
			Logger.verifyEquals(0, infos.size(), "验证返回failSendRecord为0");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_querySendFailDetail_004() {
		Logger.start(true,"获取任务信息-TaskId为空");
		try {
			querysendfaildetailBean.setAppId("testAuto");
			querysendfaildetailBean.setRequestId("testAuto");
			querysendfaildetailCall.setData(querysendfaildetailBean);
			querysendfaildetailCall.callService();	
			
			Logger.verifyEquals("0", querysendfaildetailCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", querysendfaildetailCall.getMessage(), "验证返回Message");
			List<failSendRecord> infos = querysendfaildetailCall.getfailSendRecord();
			Logger.verifyEquals(0, infos.size(), "验证返回failSendRecord为0");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyReturn(int taskId)
	{
		
		List<couponsendtaskfailrecord> failracords=new couponsendtaskfailrecordWapper().selectByTaskId(taskId);
		List<couponsendsubtask> subtasks= new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(taskId);
		Logger.verifyEquals("0", querysendfaildetailCall.getCode(), "验证返回code");
		Logger.verifyEquals("操作成功！", querysendfaildetailCall.getMessage(), "验证返回Message");
		List<failSendRecord> infos = querysendfaildetailCall.getfailSendRecord();
	    for(int i=0;i<failracords.size();i++)
	    {
	    	Logger.verifyEquals(subtasks.get(i).getUserIds(), infos.get(i).getuserId(), "验证返回userId");
	    	Logger.verifyEquals(failracords.get(i).getFailReason().toString(), infos.get(i).getsendFailReason(), "验证返回sendFailReason");
	    	Logger.verifyEquals(failracords.get(i).getRemark(), infos.get(i).getfailReasonMemo(), "验证返回failReasonMemo");
	    }
		
	}
	
	//套券优惠券-创建错误任务
	public createTaskByUserIdCall setCreateTaskByUserIdBeanForFailBagPlatform()
			throws ClientProtocolException, IOException {
	int userId=Integer.parseInt(TestDataManager.getRandomNum(5));
	
	createTaskByUserIdBean createTaskByUserIdBean=new createTaskByUserIdBean();
	
	CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
	
	CreateBagBean createBagBean = couponCallService.setDefaultPlatformCreateCouponBagBeanForRecieve();
	createBagBean.setTotalNum(2);
	CreateBagCall createBagCall = couponCallService.createPlatformCouponBag(createBagBean);
	
	userReceiveCouponBagBean userReceiveCouponBagBean=couponCallService.setuserReceiveCouponBagBean(createBagCall.getbagCode());
	userReceiveCouponBagCall userReceiveCouponBagCall=new userReceiveCouponBagCall();
	userReceiveCouponBagCall.setData(userReceiveCouponBagBean);
	userReceiveCouponBagCall.callService();
	
	List<Integer> userIds=new ArrayList<Integer>();
	userIds.add(111);
	
	createTaskByUserIdBean.setUserIds(userIds);
	createTaskByUserIdBean.setBatchType(2);
	createTaskByUserIdBean.setBatchCode(createBagCall.getbagCode());
	createTaskByUserIdBean.setCreaterId(22);
	createTaskByUserIdBean.setCreaterName("autoTest");
	createTaskByUserIdBean.setNotify(false);
	createTaskByUserIdCall call = new createTaskByUserIdCall();
	call.setData(createTaskByUserIdBean);
	call.callService();
	call.callService();
	return call;
	}
}